﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading;

using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Util;

namespace FCNS.KeepMoney.Forms
{
    public partial class LoginForm : ParentForm
    {
        public LoginForm()
        {
            InitializeComponent();

            Program.Translation.ApplyTo(this);

            SetControlLocation(new List<Control[]> { 
            new Control[]{labelName,textBoxName},
             new Control[]{labelPwd,textBoxPwd},
             new Control[]{labelPwd2,textBoxPwd2}});
            buttonOk.Left = textBoxPwd2.Left + textBoxPwd2.Width - buttonOk.Width;
        }

        AccoutField accountField;
        bool changePwd = false;
        public bool ChangePwd
        {
            set
            {
                changePwd = value;
                this.Text = LanguageUtil.更改密码;
            }
        }

        bool lockForm = false;
        public bool LockForm
        {
            set
            {
                lockForm = value;
                this.Text = LanguageUtil.窗体已锁定;
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1, Properties.Resources.login_32,
                LanguageUtil.用户权限, LanguageUtil.请输入你的用户名和密码);

                       accountField=AccoutField.GetAccountFieldByTag("kmoney");

                       textBoxName.Text = accountField.Name;
            
            if (changePwd)
            {
                labelPwd2.Visible = true;
                textBoxPwd2.Visible = true;
                buttonOk.Text = LanguageUtil.保存;
            }
            else if (!lockForm)
                Program.ShowPromptForm(this.components, textBoxName, LanguageUtil.默认账号 + ":admin   " +
                LanguageUtil.默认密码 + ":admin ");
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (textBoxPwd.Text == accountField.Password)
            {
                if (changePwd)
                {
                    accountField.Password = textBoxPwd2.Text;
                    Program.SaveKMD = true;
                }

                this.DialogResult = DialogResult.OK;
                lockForm = false;
                this.Close();
            }
            else
                labelError.Text = LanguageUtil.密码错误;
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (lockForm)
            {
                labelError.Text = LanguageUtil.请输入密码;
                e.Cancel = true;
            }
        }
    }
}
